<template>
	<view class="consumption-record-container">
		<!-- 顶部时间选择器 -->
		<view class="date-picker-container">
			<view class="date-picker-item">
				<text class="picker-label">日期:</text>
				<picker mode="date" :value="startDate" :start="'2020-01-01'" :end="currentDate"
					@change="bindStartDateChange">
					<view class="uni-input date-input">{{startDate}}</view>
				</picker>
			</view>
			<view>-</view>
			<view class="date-picker-item">
				<picker mode="date" :value="endDate" :start="'2020-01'" :end="currentDate" @change="bindEndDateChange">
					<view class="uni-input date-input">{{endDate}}</view>
				</picker>
			</view>
			<button class="query-btn" @click="fetchConsumptionRecords">查询</button>
		</view>

		<!-- 消费记录列表 -->
		<view class="record-list" v-if="recordList.length > 0">
			<view class="record-item" v-for="item in recordList" :key="item.id">
				<view class="record-icon">
					<image :src="getRecordIcon(item.type)" class="icon-img"></image>
				</view>
				<view class="record-content">
					<view class="record-title">
						<text class="type-text">{{ getRecordTypeText(item.type) }}</text>
						<text class="record-time">{{ item.create_time }}</text>
					</view>
					<text class="record-amount"
						:class="{ 'income': item.type != 0, 'expense': type == 0 }">{{ item.type != 0 ? '+' : '-' }}{{ item.money }}元</text>
				</view>
			</view>

			<!-- 加载更多 -->
			<view class="load-more" v-if="hasMore">
				<text>{{ loading ? '加载中...' : '上拉加载更多' }}</text>
			</view>
			<view class="no-more" v-else-if="recordList.length > 0">
				<text>没有更多数据了</text>
			</view>
		</view>

		<!-- 空状态 -->
		<view class="empty-state" v-if="recordList.length === 0 && !loading">
			<image src="/static/androidImg/icon_not_data.png" class="empty-img"></image>
			<text class="empty-text">暂无消费记录</text>
		</view>
	</view>
</template>

<script>
	import {
		getUserConsumptionRecord
	} from '../../api/request.js';

	export default {
		data() {
			return {
				startDate: '', // 开始日期
				endDate: '', // 结束日期
				recordList: [], // 消费记录列表
				page: 1, // 当前页码
				pageSize: 10, // 每页条数
				total: 0, // 总记录数
				hasMore: true, // 是否有更多数据
				loading: false, // 加载状态
				currentDate: '' // 当前日期
			};
		},

		onLoad() {
			// 初始化默认日期为当前月份和当前日期
			const now = new Date();
			const year = now.getFullYear();
			const month = now.getMonth() + 1;
			const day = now.getDate();
			const defaultStartDate = `${year}-${month.toString().padStart(2, '0')}-01`;
			const defaultEndDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
			this.startDate = defaultStartDate;
			this.endDate = defaultEndDate;
			this.currentDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;

			// 加载消费记录
			this.fetchConsumptionRecords();
		},

		onReachBottom() {
			// 上拉加载更多
			if (this.hasMore && !this.loading) {
				this.page++;
				this.fetchConsumptionRecords();
			}
		},

		methods: {
			// 开始日期选择变化
			bindStartDateChange(e) {
				this.startDate = e.detail.value;
			},

			// 结束日期选择变化
			bindEndDateChange(e) {
				const newEndDate = e.detail.value;
				const start = new Date(this.startDate);
				const end = new Date(newEndDate);
				console.log(start, end, end > start)
				if (end < start) {
					uni.showToast({
						title: '结束日期不能大于开始日期',
						icon: 'none'
					});
					return;
				}

				this.endDate = newEndDate;
			},

			// 获取消费记录
			fetchConsumptionRecords() {
				if (this.loading) return;

				this.loading = true;

				getUserConsumptionRecord({
					page: this.page,
					start_time: this.startDate,
					end_time: this.endDate
				}).then(res => {
					if (res.code === 1 && res.data) {
						const newData = res.data.rows || [];

						// 格式化金额为数字类型
						newData.forEach(item => {
							item.money = parseFloat(item.money);
						});

						if (this.page === 1) {
							this.recordList = newData;
						} else {
							this.recordList = [...this.recordList, ...newData];
						}

						this.total = res.data.total || 0;
						this.hasMore = this.recordList.length < this.total;
					} else {
						uni.showToast({
							title: res.msg || '获取消费记录失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('获取消费记录失败:', err);
					uni.showToast({
						title: err.msg || '网络错误',
						icon: 'none'
					});
				}).finally(() => {
					this.loading = false;
				});
			},

			// 获取记录类型文本
			getRecordTypeText(type) {
				switch (type) {
					case 0:
						return '商品消费';
					case 1:
						return '微信充值';
					case 2:
						return '支付宝充值';
					default:
						return '其他消费';
				}
			},

			// 获取记录类型图标
			getRecordIcon(type) {
				// 根据不同类型返回不同图标
				switch (type) {
					case 0:
						return '/static/androidImg/icon_goods.png';
					case 1:
					case 2:
						return '/static/androidImg/icon_pay_add.png';
					default:
						return '/static/androidImg/icon_consumption_records.png';
				}
			}
		}
	};
</script>

<style lang="scss">
	.consumption-record-container {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx;

		.date-picker-container {
			display: flex;
			align-items: center;
			justify-content: space-between;
			flex-wrap: wrap;
			gap: 15rpx;
			margin-bottom: 30rpx;
			background: #fff;
			padding: 10rpx;
			border-radius: 12rpx;

			.date-picker-item {
				flex: 1;
				min-width: 250rpx;
				display: flex;
				align-items: center;

				.picker-label {
					font-size: 28rpx;
					color: #666;
					margin-right: 10rpx;
					white-space: nowrap;
				}

				.date-input {
					flex: 1;
					height: 60rpx;
					line-height: 60rpx;
					border: 1rpx solid #eee;
					border-radius: 8rpx;
					padding: 0 15rpx;
					font-size: 28rpx;
					color: #333;
				}
			}

			.query-btn {
				width: 120rpx;
				height: 60rpx;
				background-color: #ff6b00;
				color: #fff;
				border-radius: 8rpx;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.record-list {
			padding-bottom: 20rpx;
		}

		.record-item {
			display: flex;
			align-items: center;
			background-color: #fff;
			padding: 25rpx;
			border-radius: 12rpx;
			margin-bottom: 20rpx;

			.record-icon {
				width: 80rpx;
				height: 80rpx;
				background-color: #f5f5f5;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 20rpx;

				.icon-img {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.record-content {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;

				.record-title {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 5rpx;

					.type-text {
						font-size: 30rpx;
						color: #333;
						font-weight: 500;
					}

					.record-time {
						font-size: 24rpx;
						color: #999;
					}
				}

				.record-amount {
					font-size: 32rpx;
					font-weight: bold;
				}

				.expense {
					color: #ff4d4f;
				}

				.income {
					color: #00b42a;
				}
			}
		}

		.load-more,
		.no-more {
			text-align: center;
			padding: 30rpx 0;
			color: #999;
			font-size: 28rpx;
		}

		.empty-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding-top: 200rpx;

			.empty-img {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 30rpx;
				opacity: 0.6;
			}

			.empty-text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
</style>